package com.shilin.watchsystem.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shilin.watchsystem.entity.Product;
import com.shilin.watchsystem.entity.Worder;
import com.shilin.watchsystem.form.AddOrderForm;
import com.shilin.watchsystem.form.EditOrderForm;
import com.shilin.watchsystem.mapper.OrderMapper;
import com.shilin.watchsystem.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shilin.watchsystem.service.exception.ExceptionService;
import com.shilin.watchsystem.vo.OrderPageVo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-08-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Worder> implements IOrderService {

    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST = "127.0.0.1";
    private static final String SEPARATOR = ",";

    @Resource
    OrderMapper orderMapper;
    @Autowired
    ExceptionService exceptionService;


    @Override
    public void insert(AddOrderForm form, HttpServletRequest request) {

        String ip = getIpAddr(request);

        List<Worder> orders = orderMapper.getByIp(ip);
        if(orders.size()>0){
            exceptionService.throwException(777,"今天已经提交过订单，我们会给您致电！");
        }
        Worder order = new Worder();
        BeanUtils.copyProperties(form,order);
        order.setOrderNumber(getOrderIdByUUId());
        order.setNumber(1);
        order.setBuyerIp(ip);
        order.setStatus(0);
        save(order);
    }

    @Override
    public void update(EditOrderForm form) {
        Worder worder = getById(form.getId());
        BeanUtils.copyProperties(form,worder);
        updateById(worder);
    }

    @Override
    public IPage<OrderPageVo> page(Page<Worder> page) {

        return orderMapper.page(page);
    }

    @Override
    public OrderPageVo select(Long id) {
        return orderMapper.select(id);
    }

    public static String getOrderIdByUUId() {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        return  machineId+ String.format("%015d", hashCodeV);
    }

    public static String getIpAddr(HttpServletRequest request) {
        System.out.println(request);
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (LOCALHOST.equals(ipAddress)) {
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            // "***.***.***.***".length()
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(SEPARATOR) > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }
}
